package github.kawaiior.yuanchuxiuzhen.common.container;

import github.kawaiior.yuanchuxiuzhen.common.item.misc.ManaStoneItem;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.GongFaCreateTileEntity;
import github.kawaiior.yuanchuxiuzhen.core.init.BlockInit;
import github.kawaiior.yuanchuxiuzhen.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;

public class GongFaCreateContainer extends Container {

    public final GongFaCreateTileEntity tileEntity;

    private final IWorldPosCallable ableToInteract;

    public GongFaCreateContainer(final int id, final PlayerInventory playerInventory, final GongFaCreateTileEntity entity){
        super(ContainerInit.GONG_FA_CREATE_CONTAINER.get(), id);
        this.tileEntity = entity;
        this.ableToInteract = IWorldPosCallable.of(Objects.requireNonNull(tileEntity.getWorld()),tileEntity.getPos());


        for (int i = 0; i < GongFaCreateTileEntity.LENGTH; i++) {
            this.addSlot(new Slot(entity.getGongfaInventory(), i, i*18+44, 21));
        }

        this.addSlot(new Slot(entity.getManaInventory(), 0, 80, 57));

        // add player inventory
        for(int i =0;i<9;i++){
            this.addSlot(new Slot(playerInventory,i,i*18+8,152-10));
            this.addSlot(new Slot(playerInventory,i+9,i*18+8,94-10));
            this.addSlot(new Slot(playerInventory,i+18,i*18+8,112-10));
            this.addSlot(new Slot(playerInventory,i+27,i*18+8,130-10));
        }
    }

    public GongFaCreateContainer(final int id, final PlayerInventory playerInventory, final PacketBuffer data) {
        this(id, playerInventory, getTileEntity(playerInventory,data));
    }

    public static GongFaCreateTileEntity getTileEntity(PlayerInventory playerInventory, PacketBuffer data){
        Objects.requireNonNull(playerInventory,"玩家物品栏不能为空");
        Objects.requireNonNull(data,"数据包不能为空");

        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity = playerInventory.player.world.getTileEntity(blockPos);

        if(tileEntity instanceof GongFaCreateTileEntity){
            return (GongFaCreateTileEntity) tileEntity;
        }
        throw new IllegalStateException("invalid tile entity");
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(ableToInteract, playerIn, BlockInit.GONG_FA_CREATE_BLOCK.get());
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {

        if (clickTypeIn == ClickType.PICKUP){
            return pickup(slotId, dragType, clickTypeIn, player);
        }else if (clickTypeIn == ClickType.QUICK_MOVE){
            return quickMove(slotId, player);
        }else if (clickTypeIn == ClickType.PICKUP_ALL){
            return ItemStack.EMPTY;
        }else if (clickTypeIn == ClickType.QUICK_CRAFT){
            return player.inventory.getItemStack();
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack pickup(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player){
        if (slotId > 5){
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        ItemStack playerInvStack = player.inventory.getItemStack();
        if (playerInvStack.isEmpty()){
            if (slotId < 5){
                Slot slot = this.inventorySlots.get(slotId);
                if (slot == null || !slot.canTakeStack(player)) {
                    return ItemStack.EMPTY;
                }
                ItemStack stack = slot.getStack();
                if (stack.isEmpty()){
                    return ItemStack.EMPTY;
                }
                ItemStack manaStack = this.inventorySlots.get(5).getStack();
                if (manaStack.getItem() instanceof ManaStoneItem && manaStack.getCount() > 15){
                    manaStack.shrink(16);
                    player.inventory.setItemStack(stack.copy());
                    return stack;
                }
            }
        }else {
            if (slotId < 5) {
                return playerInvStack;
            }

            if (!(playerInvStack.getItem() instanceof ManaStoneItem)){
                return playerInvStack;
            }
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack quickMove(int slotId, PlayerEntity player){

        return ItemStack.EMPTY;
    }
}
